// habitCheck.js
Page({
  data: {
    habits: [
      { id: 1, name: '吃饭', checked: false },
      { id: 2, name: '喝水', checked: false },
      { id: 3, name: '散步', checked: false }
    ],
    customHabits: []
  },
  toggleHabit: function (e) {
    const habitId = e.currentTarget.dataset.id
    const habits = this.data.habits.map(habit => {
      if (habit.id === habitId) {
        return { ...habit, checked: !habit.checked }
      }
      return habit
    })
    this.setData({ habits })
  },
  addCustomHabit: function () {
    wx.showModal({
      title: '添加自定义习惯',
      content: '请输入习惯名称',
      editable: true,
      success: (res) => {
        if (res.confirm && res.content) {
          const newCustomHabit = {
            id: Date.now(),
            name: res.content,
            checked: false
          }
          this.setData({
            customHabits: [...this.data.customHabits, newCustomHabit]
          })
        }
      }
    })
  },
  toggleCustomHabit: function (e) {
    const habitId = e.currentTarget.dataset.id
    const customHabits = this.data.customHabits.map(habit => {
      if (habit.id === habitId) {
        return { ...habit, checked: !habit.checked }
      }
      return habit
    })
    this.setData({ customHabits })
  }
})
